﻿#include "WRFlowedLayout.h"
#include <QLayoutItem>

//初始化流布局器。
WRFlowedLayout::WRFlowedLayout(QWidget *parent)
    : QLayout(parent)
{
    m_hSpace = -1;
    m_vSpace = -1;
}
//释放当前对象所占用的相关资源。
WRFlowedLayout::~WRFlowedLayout()
{
    QLayoutItem *item;
    while ((item = takeAt(0)))
    {
        delete item;
    }
}

//获取横向边界。
int WRFlowedLayout::horizontalSpacing() const
{
    if (m_hSpace >= 0) {
        return m_hSpace;
    } else {
        return smartSpacing(QStyle::PM_LayoutHorizontalSpacing);
    }
}
//获取纵向边界。
int WRFlowedLayout::verticalSpacing() const
{
    if (m_vSpace >= 0)
    {
        return m_vSpace;
    }
    else
    {
        return smartSpacing(QStyle::PM_LayoutVerticalSpacing);
    }
}

//获取项目数量。
int WRFlowedLayout::count() const
{
    return m_itemList.size();
}
//添加子项目。
void WRFlowedLayout::addItem(QLayoutItem *item)
{
    m_itemList.append(item);
}
//获取指定位置的项目。
QLayoutItem *WRFlowedLayout::itemAt(int index) const
{
    return m_itemList.value(index);
}
//取出指定位置的项目。
QLayoutItem *WRFlowedLayout::takeAt(int index)
{
    if (index >= 0 && index < m_itemList.size())
    {
        return m_itemList.takeAt(index);
    }
    else
    {
        return 0;
    }
}

//插入元素。
void WRFlowedLayout::insertWidget(int index, QWidget* widget)
{
    if (index >= m_itemList.count())
    {
        addWidget(widget);
    }
    else
    {
        addChildWidget(widget);
        if (index < 0)
        {
            index = 0;
        }
        QWidgetItem* item = new QWidgetItemV2(widget);
        m_itemList.insert(index, item);
    }
}

//获取是否可以扩展空间。
Qt::Orientations WRFlowedLayout::expandingDirections() const
{
    return 0;
}

//获取最小尺寸。
QSize WRFlowedLayout::minimumSize() const
{
    QSize size;
    QLayoutItem *item;
    foreach (item, m_itemList)
    {
        size = size.expandedTo(item->minimumSize());
    }
    size += QSize(2*margin(), 2*margin());
    return size;
}
//获取是否需要根据宽带计计算高度。
bool WRFlowedLayout::hasHeightForWidth() const
{
    return true;
}
//获取指定宽度的需要高度。
int WRFlowedLayout::heightForWidth(int width) const
{
    int height = doLayout(QRect(0, 0, width, 0), true);
    return height;
}
//设置矩形空间。
void WRFlowedLayout::setGeometry(const QRect &rect)
{
    QLayout::setGeometry(rect);
    doLayout(rect, false);
}
//获取尺寸建议。
QSize WRFlowedLayout::sizeHint() const
{
    return minimumSize();
}

//执行布局。
int WRFlowedLayout::doLayout(const QRect &rect, bool testOnly) const
{
    int left, top, right, bottom;
    getContentsMargins(&left, &top, &right, &bottom);
    QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom);
    int x = effectiveRect.x();
    int y = effectiveRect.y();
    int lineHeight = 0;

    QLayoutItem *item;
    foreach (item, m_itemList)
    {
        QWidget *wid = item->widget();
        int spaceX = horizontalSpacing();
        if (spaceX == -1)
            spaceX = wid->style()->layoutSpacing(
                QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal);
        int spaceY = verticalSpacing();
        if (spaceY == -1)
            spaceY = wid->style()->layoutSpacing(
                QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
        int nextX = x + item->sizeHint().width() + spaceX;
        if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) {
            x = effectiveRect.x();
            y = y + lineHeight + spaceY;
            nextX = x + item->sizeHint().width() + spaceX;
            lineHeight = 0;
        }

        if (!testOnly)
            item->setGeometry(QRect(QPoint(x, y), item->sizeHint()));

        x = nextX;
        lineHeight = qMax(lineHeight, item->sizeHint().height());
    }
    return y + lineHeight - rect.y() + bottom;
}
//智能获取间距。
int WRFlowedLayout::smartSpacing(QStyle::PixelMetric pm) const
{
    QObject *parent = this->parent();
    if (!parent) {
        return -1;
    } else if (parent->isWidgetType()) {
        QWidget *pw = static_cast<QWidget *>(parent);
        return pw->style()->pixelMetric(pm, 0, pw);
    } else {
        return static_cast<QLayout *>(parent)->spacing();
    }
}


